﻿singleApp.controller('registerCtrl', ['$http', '$scope', function ($http, $scope) {
    $scope.vm = {
        username: "",
        student_id: "",
        tutor: "",
        phone: "",
        pass: "",
        repass: "",
        group_id: "",
        group_name: "",
    };
    $scope.machine_group_list = [];

    //get machine group
    $scope.init_machine_group = function () {
        layer.load(2);

        var url = window.singleConfig.rootUrl + "/api/user/CommonService/getAllMachine";
        $http.get(url).then(function (result) {
            $scope.machine_group_list = result.data.data;
            $scope.vm.group_id = $scope.machine_group_list[0].id;
            layer.closeAll();
        }, function () { });
    };

    $scope.init_machine_group();

    $scope.submit = function () {
        if ($scope.vm.username == "") {
            layer.msg("请输入真实姓名 !");
            return;
        }
        if ($scope.vm.phone == "") {
            layer.msg("请输入手机号 !");
            return;
        }
        if ($scope.vm.student_id == "") {
            layer.msg("请输入学号 !");
            return;
        }
        if ($scope.vm.tutor == "") {
            layer.msg("请输入导师姓名 !");
            return;
        }
        if ($scope.vm.pass == "") {
            layer.msg("请输入密码 !");
            return;
        }
        if ($scope.vm.repass == "") {
            layer.msg("请输入密码 !");
            return;
        }

        if ($scope.vm.pass != $scope.vm.repass) {
            layer.msg("两次密码输入不一致，请重新输入!");
            return;
        }

        angular.forEach($scope.machine_group_list, function (data, index, array) {
            if (data.id == $scope.vm.group_id) {
                $scope.vm.group_name = data.name;
            }
        });

        api_url = window.singleConfig.rootUrl + 'api/user/AuthService/registerPost';
        layer.load(2);
        $http({
            method: 'POST',
            url: api_url,
            data: $.param($scope.vm),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
        .then(function (result) {
            var data = result.data;
            if (data.code == "success") {
                layer.closeAll();
                layer.msg("注册成功!");
                setTimeout(function () {
                    //location.href = window.singleConfig.rootUrl + "auth/register_success";
                    location.href = window.singleConfig.rootUrl + "user/profile";
                }, 500);
            }
            else {
                layer.closeAll();
                layer.msg(data.message);
            }
        }, function (data) {
            layer.closeAll();
            layer.msg("注册失败!");
        });



    };
}]);